/**
 * Created by yuris on 19.12.14.
 */
package view {
import ShopObjects.CashDesk1View;

import components.CashDeskComponent;

import flash.events.TimerEvent;

import flash.utils.Timer;

import messages.ControllerMessage;
import messages.ModelToViewMessage;

import starling.events.Event;

public class CashDeskComponentMediator extends AbstractComponentMediator{
    [Inject]
    public var cashDeskComponent:CashDeskComponent;
    private var _cashdeskView:CashDesk1View;

    private var _secondTimer:Timer = new Timer(1000);
    override protected function onRegister():void {
        super._createViewAsStaticObject(cashDeskComponent);
        this._cashdeskView = cashDeskComponent.view as CashDesk1View;

        cashDeskComponent.model.updateSignal.add(this._onDataUpdate);
        cashDeskComponent.view.addEventListener(Event.REMOVED_FROM_STAGE,this.HANDLER_removedFromStage);
        this._secondTimer.start();
        addHandler(ModelToViewMessage.TIME_UPDATE, this.HANDLER_updateTime);
    }

    private function HANDLER_removedFromStage(event:Event):void {
        mediatorMap.unmediate(cashDeskComponent);
    }

    override protected function onRemove():void {
        cashDeskComponent.model.updateSignal.remove(this._onDataUpdate);
        cashDeskComponent.view.removeEventListener(Event.REMOVED_FROM_STAGE,this.HANDLER_removedFromStage);
        this._secondTimer.stop();
    }





    private function _onDataUpdate(commandName:String, param:Object):void {
        //-------------
        //TODO: do update according to commandName and param
        //--------------


        this._placeView(cashDeskComponent);
    }

    private var _bufFaceTimerCounter:int = 0;
    private function HANDLER_updateTime(param:Object):void {
        this._bufFaceTimerCounter++;
        if(_bufFaceTimerCounter > 1){
            this._bufFaceTimerCounter = 0;
            if(Math.random() < 0.3){
                this._cashdeskView.changeCashManFaceAnimation();
            } else if(Math.random() < 0.9){
                this._cashdeskView.updateFaceAnimation();
            }
        }
    }
}
}
